#!/usr/bin/env bash

INSTANCE=""
OUTFILE=playlist.json
PLID=1
TOKEN=""
while getopts "i:o:p:t:" arg; do
	case $arg in
		i)
			INSTANCE=${OPTARG}
			;;
		p)
			PLID=${OPTARG}
			;;
		o)
			OUTFILE=${OPTARG}
			;;
		t)
			TOKEN=${OPTARG}
			;;
	esac
done
shift $((OPTIND-1))

if [ -z "${INSTANCE}" ]; then
    echo No instance given
	exit 1
fi

if [ -z "${TOKEN}" ]; then
    echo No token given
	exit 1
fi

plurl="https://${INSTANCE}/api/v1/playlists/${PLID}/"

answer="$(curl -s -X 'GET' -H 'Authorization: Bearer '${TOKEN} -H 'accept: application/json' ${plurl})"
if [ "$(echo ${answer}|jq -r '.id')" != "${PLID}" ]; then
	echo Could not grab the right Playlist ${PLID} from instance ${INSTANCE}
	exit 1
fi

curl -s -X 'DELETE' -H 'Authorization: Bearer '${TOKEN} -H 'accept: */*' ${plurl}

